package database

import kotlinx.serialization.Serializable

enum class DbSets {
    IotDb, Sqlite, MongoDb, Oracle, Redis
}

@Serializable
sealed class DataProperty {
    abstract var id: String
    abstract var name: String
    abstract var ip: String
    abstract var port: Int
    abstract var username: String
    abstract var password: String
    abstract fun useProperty(): MutableMap<String, String>
}

data class TimeSeries (
    val name: String,
    val values: MutableMap<Long, Float>
)

data class TimeSeriesList (
    var name: String,
    val timeSeries: MutableList<TimeSeries>
)
//compile 'org.mongodb:mongodb-driver:3.3.0'

/**   这里遗留了抽象方法的问题，先不着急解决    **/
interface DataMaster {
    val dataProperty: DataProperty

    companion object Factory {
        fun instance(dp: DataProperty): DataMaster = when (dp) {
            is IotDbProperty -> IotDbFactory(dp)
            is SqliteProperty -> SqliteFactory(dp)
            is MongoDbProperty -> MongoDbFactory(dp)
        }
    }
}

fun TimeSeries.print() {
    println("---------------------  ${this.name}   --------------------")
    this.values.forEach { time, value ->
        println("($time , $value)")
    }
    println("------------------------------------------------------------")
}

fun TimeSeriesList.print() {
    println("---------------------  ${this.name}   --------------------")
    this.timeSeries.forEach { ts ->
        println("***********   ${ts.name}    ***********")
        ts.values.forEach { time, value ->
            println("($time , $value)")
        }
        println("********************************")
    }
    println("------------------------------------------------------------")
}

fun main() {
    val property = mapOf(
        Pair("id", "iotdb"),
        Pair("name", "本地 iotdb 数据库"),
        Pair("ip", "127.0.0.1"),
        Pair("port", "6667"),
        Pair("username", "root"),
        Pair("password", "root"),
        Pair("fetchSize", "1000"),
        Pair("enableRPC", "false"),
        Pair("version", "V_1_0"),
    )
    val dp = IotDbProperty("iotdb","本地 iotdb 数据库", "127.0.0.1", 6667, "root", "root").apply {
        fetchSize = 1024
    }
    val db = DataMaster.instance(dp) as IotDbFactory
    db.connect()
}